package com.foreknow.service.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    private GuestbookDao guestbookDao;

    public GuestbookDao getGuestbookDao() {
        return guestbookDao;
    }

    public void setGuestbookDao(GuestbookDao guestbookDao) {
        this.guestbookDao = guestbookDao;
    }
//添加
    @Override
    public int add(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        jdbcTemplate.setConnection(conn);
        GuestbookDao guestbookDao = new GuestbookDaoImpl();
        int a = 0;
        try {
            a = guestbookDao.add(guestbook);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return a;
    }
//修改
    @Override
    public int update(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        jdbcTemplate.setConnection(conn);
        GuestbookDao guestbookDao = new GuestbookDaoImpl();
        int a = 0;
        try {
            a = guestbookDao.update(guestbook);
            conn.commit();
            return a;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return 0;
    }
//删除
    @Override
    public int delete(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        jdbcTemplate.setConnection(conn);
        GuestbookDao guestbookDao = new GuestbookDaoImpl();
        int a = 0;
        try {
            a = guestbookDao.delete(id) ;
            conn.commit();
            return a;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return a;
    }
//查询
    @Override
    public Guestbook query(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        jdbcTemplate.setConnection(conn);
        GuestbookDao guestbookDao = new GuestbookDaoImpl();
        try {
            return guestbookDao.query(id);
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return null;
    }
//模糊查询
    @Override
    public List<Object> mohuquery(String name) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        jdbcTemplate.setConnection(conn);
        GuestbookDao guestbookDao = new GuestbookDaoImpl();
        try {
            return guestbookDao.mohuquery(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Object> allquery() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        jdbcTemplate.setConnection(conn);
        GuestbookDao guestbookDao = new GuestbookDaoImpl();
        List<Object> list = guestbookDao.allquery();
        return list;
    }

    public static void main(String[] args) {
        GuestbookServiceImpl guestbookService = new GuestbookServiceImpl();
        Guestbook guestbook = new Guestbook();

        List<Object> list = guestbookService.allquery();
        for (int i = 0 ;i<list.size();i++){
            Guestbook gg = (Guestbook) list.get(i);
            System.out.println(gg.getId()+"--"+ gg.getName());
        }

//        List<Object> list = guestbookService.mohuquery("f");
//       for (int i = 0 ;i<list.size();i++){
//           Guestbook gg = (Guestbook) list.get(i);
//           System.out.println(gg.getId()+"--"+ gg.getName());
//       }
///////       改一下
        Guestbook guestbook1 = new Guestbook();
        guestbook1.setName("tom");
        guestbook1.setPhone("15456451");
        guestbook1.setEmail("45465@qq.saf");
        guestbook1.setTitle("test");
        guestbook1.setContent("safddgsdas");
        guestbook1.setCreatedtime("2021-12-11");
        int a = guestbookService.update(guestbook1);
        System.out.println(a);

//        guestbook.setName("tony");
//        int a = guestbookService.add(guestbook);
//        System.out.println(a);

//        int b = guestbookService.delete(13);
//        System.out.println(b);

//        guestbook = guestbookService.query(9);
//        System.out.println(guestbook.getName());

    }
}
